package com.TeacherInfoServer.controller;

import com.TeacherInfoServer.common.Result;
import com.TeacherInfoServer.listener.TitleHistoryListener;
import com.TeacherInfoServer.pojo.DTO.AddTitleHistoryDTO;
import com.TeacherInfoServer.pojo.DTO.SearchDTO.TitleHistorySearchDTO;
import com.TeacherInfoServer.pojo.DTO.UpdateTitleHistoryDTO;
import com.TeacherInfoServer.pojo.excelImport.TitleHistoryExcel;
import com.TeacherInfoServer.pojo.vo.TitleHistoryListVo;
import com.TeacherInfoServer.common.service.TitleHistoryService;
import com.alibaba.excel.EasyExcel;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/s1/title_history")
public class TitleHistoryController {

    @Autowired
    private TitleHistoryService titleHistoryService;
    @Autowired
    private TitleHistoryListener titleHistoryListener;

    @PostMapping("/addOne")
    public Result<Map<String, Integer>> addOne(@RequestBody @Valid AddTitleHistoryDTO addTitleHistoryDTO) {
        return titleHistoryService.addOne(addTitleHistoryDTO);
    }

    @PostMapping("/addBatch")
    public Result<Map<String,Integer>> addBatch(@RequestBody MultipartFile file) throws IOException{
        titleHistoryListener.initList();
        EasyExcel.read(file.getInputStream(), TitleHistoryExcel.class,titleHistoryListener).sheet().doRead();
        Map<String,Integer> map = new HashMap<>();
        map.put("record",titleHistoryListener.getRecord());
        return Result.success(map);
    }

    @GetMapping(value = "/getList", params = {"pageNumber", "pageSize"})
    public Result<TitleHistoryListVo> getList(@RequestParam Long pageNumber, @RequestParam Long pageSize) {
        return titleHistoryService.getList(pageNumber, pageSize);
    }

    @Operation(summary = "模糊查询获取教师职称历史信息列表",description = "模糊查询分页获取教师职称历史信息列表")
    @PostMapping("/searchList")
    public Result<Map<String,Object>> searchList(@RequestBody TitleHistorySearchDTO titleHistorySearchDTO){
        return Result.success("获取教师学习经历列表成功",titleHistoryService.searchTitleHistoryList(titleHistorySearchDTO));
    }

//    @GetMapping(value = "getOneList", params = {"pageNumber", "pageSize","teacherId"})
//    public Result<TitleHistoryListVo> getOneList(@RequestParam Long pageNumber, @RequestParam Long pageSize,@RequestParam String teacherId){
//        return titleHistoryService.getOneList(pageNumber,pageSize,teacherId);
//    }

    @GetMapping(value = "/export",params = "fileName")
    public void export(HttpServletResponse response,@RequestParam String fileName) throws IOException{
        titleHistoryService.export(response,fileName);
    }

    @DeleteMapping(value = "/delete",params = "ids")
    public Result<Map<String,Integer>> deleteBatch(@RequestParam Integer[] ids){
        return titleHistoryService.deleteBatch(ids);
    }

    @PutMapping("/update")
    public Result<Map<String,Integer>> updateOne(@RequestBody @Valid UpdateTitleHistoryDTO updateTitleHistoryDTO){
        return titleHistoryService.updateOne(updateTitleHistoryDTO);
    }
}